package epam.marchenko.library.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import epam.marchenko.library.domain.OutcomeRecord;
import epam.marchenko.library.domain.User;
import epam.marchenko.library.services.OutcomeRecordService;
import epam.marchenko.library.services.UserService;

/**
 * Handles requests for the application home page.
 */
@Controller
@RequestMapping("/user")
public class UserController {
	
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	
	UserService userService;
	OutcomeRecordService outcomeService;
	
	@Autowired
	public UserController(UserService userService, OutcomeRecordService outcomeService) {
		this.userService = userService;
		this.outcomeService = outcomeService;
	}
		
	@RequestMapping(method = RequestMethod.GET)
	public String homepage(Model model) {
		logger.info("Welcome to homepage");
		
		String loggedUsername = SecurityContextHolder.getContext().getAuthentication().getName();
		User loggedUser = userService.findUserByLogin(loggedUsername);
		model.addAttribute("user",loggedUser);
		List<OutcomeRecord> orders = outcomeService.getActiveOrdersForUser(loggedUser);
		model.addAttribute("orders", orders);
		return "user";
	}
}
